package com.bigdata.movieticketapi.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata.movieticketapi.common.exception.BusinessException;
import com.bigdata.movieticketapi.common.exception.ResponseCode;
import com.bigdata.movieticketapi.common.pojo.PageResult;
import com.bigdata.movieticketapi.entity.Movie;
import com.bigdata.movieticketapi.mapper.MovieMapper;
import com.bigdata.movieticketapi.service.IMovieService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.movieticketapi.vo.req.GetDataByIdReqVo;
import com.bigdata.movieticketapi.vo.req.MovieReqVO;
import com.bigdata.movieticketapi.vo.req.PageMovieReqVO;
import com.bigdata.movieticketapi.vo.resp.MovieDetailRespVO;
import com.bigdata.movieticketapi.vo.resp.SelectListRespVO;
import com.bigdata.movieticketapi.vo.resp.PageMovieRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author movie
 * @since 2023-10-26
 */
@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements IMovieService {

    @Resource
    private MovieMapper movieMapper;

    @Override
    public void addMovie(MovieReqVO vo) {

        Movie movie = query().select("id").last("limit 1").eq("chinese_name", vo.getChineseName()).one();
        if(movie != null){
            throw new BusinessException(ResponseCode.MOVIE_ALREADY_EXISTS.getCode(), ResponseCode.MOVIE_ALREADY_EXISTS.getMessage());
        }
        Movie insertMovie = new Movie();
        BeanUtils.copyProperties(vo,insertMovie);
        int result = movieMapper.insert(insertMovie);
        if (result !=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public void updateMovie(Movie movie) {
        int result = movieMapper.updateById(movie);
        if (result !=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public MovieDetailRespVO getMovieDetail(GetDataByIdReqVo vo) {
        return movieMapper.selectMovieDetail(vo);
    }

    @Override
    public PageResult<PageMovieRespVO> pageMovies(PageMovieReqVO vo) {
        IPage<PageMovieRespVO> result = movieMapper.selectPageMovie(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(),result.getTotal());
    }

    @Override
    public PageResult<PageMovieRespVO> uniAppPageMovie(PageMovieReqVO vo) {
        IPage<PageMovieRespVO> result = movieMapper.selectUniAppPageMovie(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(),result.getTotal());
    }

    @Override
    public List<SelectListRespVO> getMovieSelectList() {
        return movieMapper.selectMovieSelectList();
    }
}
